package com.kedacom.emptyproject.map.widget

import android.animation.ValueAnimator
import android.content.Context
import android.graphics.*
import android.support.annotation.IntDef
import android.text.TextPaint
import android.util.AttributeSet
import android.view.MotionEvent
import android.view.View
import com.kedacom.emptyproject.R
import com.kedacom.emptyproject.map.widget.MapLayerBottomTabView.LayerStatus.Companion.CLOSE
import com.kedacom.emptyproject.map.widget.MapLayerBottomTabView.LayerStatus.Companion.NORMAL
import kotlin.math.abs

/**
 * 底部导航栏
 * @author GuanHaoran
 * @description:
 * @date :2020/9/29 2:41 PM
 */
class MapLayerBottomTabView(context: Context, attr: AttributeSet? = null) : View(context, attr) {
    constructor(cxt: Context) : this(context = cxt)

    @IntDef(flag = true, value = [CLOSE, /*EXPANDED,*/ NORMAL])
    @Retention(AnnotationRetention.SOURCE)
    annotation class LayerStatus {
        companion object {
            const val CLOSE = 0

            //            const val EXPANDED = 1
            const val NORMAL = 2
        }
    }

    private var WIDTH = 0
    private var HEIGHT = 0

    private var roleArray = mutableListOf<String>()

    private val homeBitmap: Bitmap =
        BitmapFactory.decodeResource(resources, R.mipmap.layer_bottom_home)

    private var leftBitmapUnsel: Bitmap =
        BitmapFactory.decodeResource(resources, R.mipmap.layer_bot_msg_unsel)
    private var leftBitmapSel: Bitmap =
        BitmapFactory.decodeResource(resources, R.mipmap.layer_bot_msg_sel)

    private var rightBitmapUnsel: Bitmap =
        BitmapFactory.decodeResource(resources, R.mipmap.layer_bot_dd_unsel)
    private var rightBitmapSel: Bitmap =
        BitmapFactory.decodeResource(resources, R.mipmap.layer_bot_dd_sel)

    private val homeBimWidth = homeBitmap.width
    private val homeBimHeight = homeBitmap.height


    private var currentState = NORMAL
    private var endState = NORMAL
    private lateinit var currentStatePath: Path
    private lateinit var currentLRectF: RectF
    private lateinit var currentRRectF: RectF
    private var LabelRect: Rect = Rect()

    private var CLOSE_START_X = 0f
    private var CLOSE_TOP_Y = 0f
    private var CLOSE_END_X = 0f
    private var CLOSE_BOTTOM_Y = 0f

    private var EXPANDED_START_X = 0f
    private var EXPANDED_TOP_Y = 0f
    private var EXPANDED_END_X = 0f
    private var EXPANDED_BOTTOM_Y = 0f

    private lateinit var bgPaint: Paint
    private lateinit var labelPaint: TextPaint
    private lateinit var labelPaintBG: Paint

    private var animValue = 0f
    private var isAnimValue = false
    private var isClickRightRF = false
    private var isClickLeftRF = false

    private var labelMsg: String? = null


    init {
        bgPaint = Paint().apply {
            color = Color.parseColor("#ffffff")
            isAntiAlias = true
            setShadowLayer(10f, 0f, 0f, Color.parseColor("#22000000"))
        }
        labelPaint = TextPaint().apply {
            color = Color.parseColor("#ffffff")
            isAntiAlias = true
            textSize = 40f
            setShadowLayer(10f, 0f, 0f, Color.parseColor("#22000000"))
        }
        labelPaintBG = Paint().apply {
            color = Color.parseColor("#ff0000")
            isAntiAlias = true
            textSize = 20f
            setShadowLayer(10f, 0f, 0f, Color.parseColor("#22000000"))
        }

//        val typedArray = context.obtainStyledAttributes(attr, R.styleable.MapLayerBottomTabView)
//        val string = typedArray.getString(R.styleable.MapLayerBottomTabView_tab_view_state2)
//        tabViewState(string)

    }

    private fun tabViewState(string: String?) {
        string?.apply {
            val split = toCharArray().map { it.toString() }
            if (split.isNotEmpty() && !split.contains("0")) {
                roleArray.addAll(split)
                //只有两个权限的时候 动态调整顺序
                if (roleArray.size == 2) {
                    when (roleArray[0]) {
                        "1" -> {
                            leftBitmapUnsel = BitmapFactory.decodeResource(
                                resources,
                                R.mipmap.layer_bot_msg_unsel
                            )
                            leftBitmapSel =
                                BitmapFactory.decodeResource(resources, R.mipmap.layer_bot_msg_sel)
                        }
                        "2" -> {
                            leftBitmapUnsel = BitmapFactory.decodeResource(
                                resources,
                                R.mipmap.layer_bot_home_unsel
                            )
                            leftBitmapSel =
                                BitmapFactory.decodeResource(resources, R.mipmap.layer_bot_home_sel)
                        }
                        else -> {
                            leftBitmapUnsel =
                                BitmapFactory.decodeResource(resources, R.mipmap.layer_bot_dd_unsel)
                            leftBitmapSel =
                                BitmapFactory.decodeResource(resources, R.mipmap.layer_bot_dd_sel)
                        }
                    }
                    when (roleArray[1]) {
                        "1" -> {
                            rightBitmapUnsel = BitmapFactory.decodeResource(
                                resources,
                                R.mipmap.layer_bot_msg_unsel
                            )
                            rightBitmapSel =
                                BitmapFactory.decodeResource(resources, R.mipmap.layer_bot_msg_sel)
                        }
                        "2" -> {
                            rightBitmapUnsel = BitmapFactory.decodeResource(
                                resources,
                                R.mipmap.layer_bot_home_unsel
                            )
                            rightBitmapSel =
                                BitmapFactory.decodeResource(resources, R.mipmap.layer_bot_home_sel)
                        }
                        else -> {
                            rightBitmapUnsel =
                                BitmapFactory.decodeResource(resources, R.mipmap.layer_bot_dd_unsel)
                            rightBitmapSel =
                                BitmapFactory.decodeResource(resources, R.mipmap.layer_bot_dd_sel)
                        }
                    }
                    //默认选中图层页面
                    if (roleArray.size == 2 && roleArray.contains("2")) {
                        if (roleArray[0] == "2")
                            isClickLeftRF = true
                        if (roleArray[1] == "2")
                            isClickRightRF = true
                    }
                }
            }
        }
    }


    override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec)
        WIDTH = measuredWidth
        HEIGHT = measuredHeight

        CLOSE_START_X = WIDTH * 0.2f
        CLOSE_TOP_Y = HEIGHT - (HEIGHT * 0.95f) + 15f
        CLOSE_END_X = WIDTH * 0.8f
        CLOSE_BOTTOM_Y = homeBimHeight - 10f


        EXPANDED_START_X = 0f
        EXPANDED_TOP_Y = HEIGHT / 2f
        EXPANDED_END_X = WIDTH.toFloat()
        EXPANDED_BOTTOM_Y = HEIGHT.toFloat()


    }

    override fun onDraw(canvas: Canvas?) {
        super.onDraw(canvas)
        canvas?.apply {

            if (roleArray.size == 0)
                return
            if (roleArray.size == 2) {
                drawPath(getExpandedPath(), bgPaint)
                drawBitmap(
                    if (isClickRightRF) rightBitmapSel else rightBitmapUnsel,
                    currentRRectF.left + (currentRRectF.right - currentRRectF.left) / 2 - leftBitmapUnsel.width / 2,
                    currentRRectF.top + (currentRRectF.bottom - currentRRectF.top) / 2 - leftBitmapUnsel.height / 2,
                    bgPaint
                )

                drawBitmap(
                    if (isClickLeftRF) leftBitmapSel else leftBitmapUnsel,
                    currentLRectF.left + (currentLRectF.right - currentLRectF.left) / 2 - leftBitmapUnsel.width / 2,
                    currentLRectF.top + (currentLRectF.bottom - currentLRectF.top) / 2 - leftBitmapUnsel.height / 2,
                    bgPaint
                )
                drawLabel(canvas)
                return
            }


            if (currentState == NORMAL && endState == CLOSE)
                drawPath(normal2Close(), bgPaint)
            if (currentState == CLOSE && endState == NORMAL)
                drawPath(close2Normal(), bgPaint)

            if (currentState == NORMAL && endState == CLOSE) {
                drawBitmap(
                    homeBitmap,
                    WIDTH / 2f - homeBimWidth / 2f,
                    HEIGHT - (homeBimHeight.toFloat()) - ((HEIGHT - (homeBimHeight.toFloat())) - 10f) * animValue,
                    bgPaint
                )
            }
            if (currentState == CLOSE && endState == NORMAL) {
                drawBitmap(
                    homeBitmap,
                    WIDTH / 2f - homeBimWidth / 2f,
                    10f - (10f - (HEIGHT - (homeBimHeight.toFloat()))) * animValue,
                    bgPaint
                )
            }

            if (currentState == endState) {
                if (currentState == CLOSE) {
                    drawPath(getClosePath(), bgPaint)
                    drawBitmap(homeBitmap, WIDTH / 2f - homeBimWidth / 2f, 10f, bgPaint)
                }

                if (currentState == NORMAL) {
                    drawPath(getNormalPath(), bgPaint)
                    drawBitmap(
                        homeBitmap,
                        WIDTH / 2f - homeBimWidth / 2f,
                        HEIGHT - (homeBimHeight.toFloat()),
                        bgPaint
                    )
                }
            }


            drawBitmap(
                if (isClickRightRF) rightBitmapSel else rightBitmapUnsel,
                currentRRectF.left + (currentRRectF.right - currentRRectF.left) / 2 - leftBitmapUnsel.width / 2,
                currentRRectF.top + (currentRRectF.bottom - currentRRectF.top) / 2 - leftBitmapUnsel.height / 2,
                bgPaint
            )

            drawBitmap(
                if (isClickLeftRF) leftBitmapSel else leftBitmapUnsel,
                currentLRectF.left + (currentLRectF.right - currentLRectF.left) / 2 - leftBitmapUnsel.width / 2,
                currentLRectF.top + (currentLRectF.bottom - currentLRectF.top) / 2 - leftBitmapUnsel.height / 2,
                bgPaint
            )
            drawLabel(canvas)

            //close状态
//            drawBitmap(homeBitmap, WIDTH / 2f - homeBimWidth / 2f, 10f, bgPaint)
            //open状态
//            drawBitmap(homeBitmap, WIDTH / 2f - homeBimWidth / 2f, HEIGHT - (homeBimHeight +20f), bgPaint)

        }
    }

    private fun drawLabel(canvas: Canvas) {
        //绘制标签
        if (!labelMsg.isNullOrEmpty()) {
            if (roleArray.size == 3) {
                if (currentState == CLOSE && endState == NORMAL) {
                    labelPaint.textSize = 20f + (40f - 20f) * animValue
                } else if (currentState == NORMAL && endState == CLOSE) {
                    labelPaint.textSize = 40f + (20f - 40f) * animValue
                }
            }

            LabelRect = Rect()
            labelPaint.getTextBounds(labelMsg, 0, labelMsg!!.length, LabelRect)
            canvas.drawCircle(
                currentLRectF.left + (currentLRectF.right - currentLRectF.left) / 2 + leftBitmapUnsel.width / 2 + LabelRect.width() / 2 - 7f,
                currentLRectF.top + (currentLRectF.bottom - currentLRectF.top) / 2 - leftBitmapUnsel.height / 4 - LabelRect.height() / 2,
                if (labelMsg!!.length == 1) LabelRect.height() / 2f + 10 else if (labelMsg!!.length == 2) LabelRect.height() / 2f + 15 else LabelRect.width() / 2f + 2,
                labelPaintBG
            )

            canvas.drawText(
                labelMsg!!,
                currentLRectF.left + (currentLRectF.right - currentLRectF.left) / 2 + leftBitmapUnsel.width / 2 - 10f,
                currentLRectF.top + (currentLRectF.bottom - currentLRectF.top) / 2 - leftBitmapUnsel.height / 4,
                labelPaint
            )
//                val measureText = labelPaint.measureText(labelMsg)

        }
    }

    /*  private fun getClosePath(): Path {
          return Path().apply {
              moveTo(CLOSE_START_X, CLOSE_TOP_Y)
              lineTo(WIDTH / 2f - homeBimWidth / 2f + 20f, CLOSE_TOP_Y)
              quadTo(WIDTH / 2f, -homeBimWidth/7f, WIDTH / 2f + homeBimWidth / 2f - 20f, CLOSE_TOP_Y)
              lineTo(CLOSE_END_X, CLOSE_TOP_Y)
              arcTo(WIDTH/2f+homeBimWidth,CLOSE_TOP_Y,CLOSE_END_X+homeBimWidth/2f,CLOSE_BOTTOM_Y,-90f,180f,false)
              lineTo(WIDTH/2f + homeBimWidth/2f-20f,CLOSE_BOTTOM_Y)
              quadTo(WIDTH / 2f, CLOSE_BOTTOM_Y+homeBimWidth/3f, WIDTH / 2f - homeBimWidth / 2f + 20f, CLOSE_BOTTOM_Y)
              lineTo(CLOSE_START_X, CLOSE_BOTTOM_Y)
              arcTo(CLOSE_START_X-homeBimWidth/2,CLOSE_TOP_Y,WIDTH/2f-homeBimWidth,CLOSE_BOTTOM_Y,90f,180f,false)
              addCircle()
          }

      } */
    private fun getClosePath(): Path {
        return Path().apply {
            val LRectF = RectF(
                CLOSE_START_X,
                CLOSE_TOP_Y,
                WIDTH / 2 - (homeBimHeight / 2f) + 20f,
                CLOSE_BOTTOM_Y
            )
            val RRectF = RectF(
                WIDTH / 2 + (homeBimHeight / 2f) - 20f,
                CLOSE_TOP_Y,
                CLOSE_END_X,
                CLOSE_BOTTOM_Y
            )

            currentLRectF = LRectF
            currentRRectF = RRectF

            addRoundRect(
                LRectF,
                floatArrayOf(100f, 100f, 0f, 0f, 0f, 0f, 100f, 100f),
                Path.Direction.CW
            )
            addRoundRect(
                RRectF,
                floatArrayOf(0f, 0f, 100f, 100f, 100f, 100f, 0f, 0f),
                Path.Direction.CW
            )

            moveTo(LRectF.right, LRectF.top + (LRectF.bottom - LRectF.top) / 2)

            cubicTo(
                LRectF.right - homeBimWidth / 2, -homeBimHeight / 5f,
                RRectF.left + homeBimWidth / 2, -homeBimHeight / 5f,
                RRectF.left, RRectF.top + (RRectF.bottom - RRectF.top) / 2
            )
            cubicTo(
                RRectF.left + homeBimWidth / 2, RRectF.bottom + homeBimHeight / 2.5f,
                LRectF.right - homeBimWidth / 2, RRectF.bottom + homeBimHeight / 2.5f,
                LRectF.right, LRectF.top + (LRectF.bottom - LRectF.top) / 2
            )
        }

    }

    private fun getExpandedPath(): Path {
        return Path().apply {
            val LRectF = RectF(
                EXPANDED_START_X,
                EXPANDED_TOP_Y,
                WIDTH / 2f,
                EXPANDED_BOTTOM_Y
            )
            val RRectF = RectF(
                WIDTH / 2f,
                EXPANDED_TOP_Y,
                EXPANDED_END_X,
                EXPANDED_BOTTOM_Y
            )

            currentLRectF = LRectF
            currentRRectF = RRectF

            addRoundRect(
                LRectF,
                floatArrayOf(
                    20f,
                    20f,
                    0f,
                    0f,
                    0f,
                    0f,
                    0f,
                    0f
                ),
                Path.Direction.CW
            )
            addRoundRect(
                RRectF,
                floatArrayOf(
                    0f,
                    0f,
                    20f,
                    20f,
                    0f,
                    0f,
                    0f,
                    0f
                ),
                Path.Direction.CW
            )

            moveTo(LRectF.right, LRectF.top + (LRectF.bottom - LRectF.top) / 2)

            /*cubicTo(
                LRectF.right + 25f, LRectF.bottom + homeBimHeight / 8f,
                RRectF.left - 25f, RRectF.bottom + homeBimHeight / 8f,
                RRectF.left, RRectF.top + (RRectF.bottom - RRectF.top) / 2
            )
            cubicTo(
                RRectF.right, RRectF.bottom + homeBimHeight / 2.5f,
                LRectF.left, RRectF.bottom + homeBimHeight / 2.5f,
                LRectF.right, LRectF.top + (LRectF.bottom - LRectF.top) / 2
            )*/
        }
    }

    private fun getNormalPath(): Path {
        return Path().apply {
            val LRectF = RectF(
                EXPANDED_START_X,
                EXPANDED_TOP_Y,
                WIDTH / 2 - (homeBimHeight / 3f),
                EXPANDED_BOTTOM_Y
            )
            val RRectF = RectF(
                WIDTH / 2 + (homeBimHeight / 3f),
                EXPANDED_TOP_Y,
                EXPANDED_END_X,
                EXPANDED_BOTTOM_Y
            )

            currentLRectF = LRectF
            currentRRectF = RRectF

            addRoundRect(LRectF, floatArrayOf(0f, 0f, 0f, 0f, 0f, 0f, 0f, 0f), Path.Direction.CW)
            addRoundRect(RRectF, floatArrayOf(0f, 0f, 0f, 0f, 0f, 0f, 0f, 0f), Path.Direction.CW)

            moveTo(LRectF.right, LRectF.top + (LRectF.bottom - LRectF.top) / 2)

            cubicTo(
                LRectF.right - homeBimWidth / 2, LRectF.top - homeBimHeight / 3f,
                RRectF.left + homeBimWidth / 2, LRectF.top - homeBimHeight / 3f,
                RRectF.left, RRectF.top + (RRectF.bottom - RRectF.top) / 2
            )

            cubicTo(
                RRectF.right, RRectF.bottom + homeBimHeight / 2.5f,
                LRectF.left, RRectF.bottom + homeBimHeight / 2.5f,
                LRectF.right, LRectF.top + (LRectF.bottom - LRectF.top) / 2
            )
        }
    }


    fun normal2Close(): Path {

        return Path().apply {
            val LRectF = RectF(
                EXPANDED_START_X - (EXPANDED_START_X - CLOSE_START_X) * animValue,
                EXPANDED_TOP_Y - (EXPANDED_TOP_Y - CLOSE_TOP_Y) * animValue,
                WIDTH / 2 - (homeBimHeight / 3f) - ((WIDTH / 2 - (homeBimHeight / 3f)) - (WIDTH / 2 - (homeBimHeight / 2f) + 20f)) * animValue,
                EXPANDED_BOTTOM_Y - (EXPANDED_BOTTOM_Y - CLOSE_BOTTOM_Y) * animValue
            )
            val RRectF = RectF(
                WIDTH / 2 + (homeBimHeight / 3f) - ((WIDTH / 2 + (homeBimHeight / 3f)) - (WIDTH / 2 + (homeBimHeight / 2f) - 20f)) * animValue,
                EXPANDED_TOP_Y - (EXPANDED_TOP_Y - CLOSE_TOP_Y) * animValue,
                EXPANDED_END_X - (EXPANDED_END_X - CLOSE_END_X) * animValue,
                EXPANDED_BOTTOM_Y - (EXPANDED_BOTTOM_Y - CLOSE_BOTTOM_Y) * animValue
            )

            currentLRectF = LRectF
            currentRRectF = RRectF



            addRoundRect(
                LRectF,
                floatArrayOf(
                    0f - (0f - 100f) * animValue,
                    0f - (0f - 100f) * animValue,
                    0f,
                    0f,
                    0f,
                    0f,
                    0f - (0f - 100f) * animValue,
                    0f - (0f - 100f) * animValue
                ),
                Path.Direction.CW
            )
            addRoundRect(
                RRectF,
                floatArrayOf(
                    0f,
                    0f,
                    0f - (0f - 100f) * animValue,
                    0f - (0f - 100f) * animValue,
                    0f - (0f - 100f) * animValue,
                    0f - (0f - 100f) * animValue,
                    0f,
                    0f
                ),
                Path.Direction.CW
            )


            moveTo(LRectF.right, LRectF.top + (LRectF.bottom - LRectF.top) / 2)


            val _1ScubX1 = LRectF.right - homeBimWidth / 2
            val _1ScubY1 = LRectF.top - homeBimHeight / 3f
            val _1ScubX2 = RRectF.left + homeBimWidth / 2
            val _1ScubY2 = LRectF.top - homeBimHeight / 3f
            val _1ScubX3 = RRectF.left
            val _1ScubY3 = RRectF.top + (RRectF.bottom - RRectF.top) / 2

            val _1EcubX1 = LRectF.right - homeBimWidth / 2
            val _1EcubY1 = -homeBimHeight / 5f
            val _1EcubX2 = RRectF.left + homeBimWidth / 2
            val _1EcubY2 = -homeBimHeight / 5f
            val _1EcubX3 = RRectF.left
            val _1EcubY3 = RRectF.top + (RRectF.bottom - RRectF.top) / 2
            cubicTo(
                _1ScubX1 - ((_1ScubX1) - (_1EcubX1)) * animValue,
                _1ScubY1 - ((_1ScubY1) - (_1EcubY1)) * animValue,
                _1ScubX2 - ((_1ScubX2) - (_1EcubX2)) * animValue,
                _1ScubY2 - ((_1ScubY2) - (_1EcubY2)) * animValue,
                _1ScubX3 - ((_1ScubX3) - (_1EcubX3)) * animValue,
                _1ScubY3 - ((_1ScubY3) - (_1EcubY3)) * animValue
            )
            val _2ScubX1 = RRectF.right
            val _2ScubY1 = RRectF.bottom + homeBimHeight / 2.5f
            val _2ScubX2 = LRectF.left
            val _2ScubY2 = RRectF.bottom + homeBimHeight / 2.5f
            val _2ScubX3 = LRectF.right
            val _2ScubY3 = LRectF.top + (LRectF.bottom - LRectF.top) / 2

            val _2EcubX1 = RRectF.left + homeBimWidth / 2
            val _2EcubY1 = RRectF.bottom + homeBimHeight / 2.5f
            val _2EcubX2 = LRectF.right - homeBimWidth / 2
            val _2EcubY2 = RRectF.bottom + homeBimHeight / 2.5f
            val _2EcubX3 = LRectF.right
            val _2EcubY3 = LRectF.top + (LRectF.bottom - LRectF.top) / 2

            cubicTo(
                _2ScubX1 - (_2ScubX1 - _2EcubX1) * animValue,
                _2ScubY1 - (_2ScubY1 - _2EcubY1) * animValue,
                _2ScubX2 - (_2ScubX2 - _2EcubX2) * animValue,
                _2ScubY2 - (_2ScubY2 - _2EcubY2) * animValue,
                _2ScubX3 - (_2ScubX3 - _2EcubX3) * animValue,
                _2ScubY3 - (_2ScubY3 - _2EcubY3) * animValue
            )
        }

    }

    fun close2Normal(): Path {

        return Path().apply {
            val LRectF = RectF(
                CLOSE_START_X - (CLOSE_START_X - EXPANDED_START_X) * animValue,
                CLOSE_TOP_Y - (CLOSE_TOP_Y - EXPANDED_TOP_Y) * animValue,
                (WIDTH / 2 - (homeBimHeight / 2f) + 20f) - ((WIDTH / 2 - (homeBimHeight / 2f) + 20f) - (WIDTH / 2 - (homeBimHeight / 3f))) * animValue,
                CLOSE_BOTTOM_Y - (CLOSE_BOTTOM_Y - EXPANDED_BOTTOM_Y) * animValue
            )
            val RRectF = RectF(
                (WIDTH / 2 + (homeBimHeight / 2f) - 20f) - ((WIDTH / 2 + (homeBimHeight / 2f) - 20f) - (WIDTH / 2 + (homeBimHeight / 3f))) * animValue,
                CLOSE_TOP_Y - ((CLOSE_TOP_Y) - (EXPANDED_TOP_Y)) * animValue,
                CLOSE_END_X - ((CLOSE_END_X) - (EXPANDED_END_X)) * animValue,
                CLOSE_BOTTOM_Y - ((CLOSE_BOTTOM_Y) - (EXPANDED_BOTTOM_Y)) * animValue
            )

            currentLRectF = LRectF
            currentRRectF = RRectF



            addRoundRect(
                LRectF,
                floatArrayOf(
                    100f - (100 * animValue),
                    100f - (100 * animValue),
                    0f,
                    0f,
                    0f,
                    0f,
                    100f - (100 * animValue),
                    100f - (100 * animValue)
                ),
                Path.Direction.CW
            )
            addRoundRect(
                RRectF,
                floatArrayOf(
                    0f,
                    0f,
                    100f - (100 * animValue),
                    100f - (100 * animValue),
                    100f - (100 * animValue),
                    100f - (100 * animValue),
                    0f,
                    0f
                ),
                Path.Direction.CW
            )

            moveTo(LRectF.right, LRectF.top + (LRectF.bottom - LRectF.top) / 2)

            val _1ScubX1 = LRectF.right - homeBimWidth / 2
            val _1ScubY1 = -homeBimHeight / 5f
            val _1ScubX2 = RRectF.left + homeBimWidth / 2
            val _1ScubY2 = -homeBimHeight / 5f
            val _1ScubX3 = RRectF.left
            val _1ScubY3 = RRectF.top + (RRectF.bottom - RRectF.top) / 2

            val _1EcubX1 = LRectF.right - homeBimWidth / 2
            val _1EcubY1 = LRectF.top - homeBimHeight / 3f
            val _1EcubX2 = RRectF.left + homeBimWidth / 2
            val _1EcubY2 = LRectF.top - homeBimHeight / 3f
            val _1EcubX3 = RRectF.left
            val _1EcubY3 = RRectF.top + (RRectF.bottom - RRectF.top) / 2
            cubicTo(
                _1ScubX1 - ((_1ScubX1) - (_1EcubX1)) * animValue,
                _1ScubY1 - ((_1ScubY1) - (_1EcubY1)) * animValue,
                _1ScubX2 - ((_1ScubX2) - (_1EcubX2)) * animValue,
                _1ScubY2 - ((_1ScubY2) - (_1EcubY2)) * animValue,
                _1ScubX3 - ((_1ScubX3) - (_1EcubX3)) * animValue,
                _1ScubY3 - ((_1ScubY3) - (_1EcubY3)) * animValue
            )
            val _2ScubX1 = RRectF.left + homeBimWidth / 2
            val _2ScubY1 = RRectF.bottom + homeBimHeight / 2.5f
            val _2ScubX2 = LRectF.right - homeBimWidth / 2
            val _2ScubY2 = RRectF.bottom + homeBimHeight / 2.5f
            val _2ScubX3 = LRectF.right
            val _2ScubY3 = LRectF.top + (LRectF.bottom - LRectF.top) / 2
            val _2EcubX1 = RRectF.right
            val _2EcubY1 = RRectF.bottom + homeBimHeight / 2.5f
            val _2EcubX2 = LRectF.left
            val _2EcubY2 = RRectF.bottom + homeBimHeight / 2.5f
            val _2EcubX3 = LRectF.right
            val _2EcubY3 = LRectF.top + (LRectF.bottom - LRectF.top) / 2


            cubicTo(
                _2ScubX1 - (_2ScubX1 - _2EcubX1) * animValue,
                _2ScubY1 - (_2ScubY1 - _2EcubY1) * animValue,
                _2ScubX2 - (_2ScubX2 - _2EcubX2) * animValue,
                _2ScubY2 - (_2ScubY2 - _2EcubY2) * animValue,
                _2ScubX3 - (_2ScubX3 - _2EcubX3) * animValue,
                _2ScubY3 - (_2ScubY3 - _2EcubY3) * animValue
            )
        }

    }


    var downX = 0f
    var downY = 0f
    private var startTime = 0L
    override fun onTouchEvent(event: MotionEvent?): Boolean {
        when (event?.action) {

            MotionEvent.ACTION_DOWN -> {
                downX = event.x
                downY = event.y
            }
            MotionEvent.ACTION_MOVE -> {

            }
            MotionEvent.ACTION_UP -> {
                if (abs(event.x - downX) <= 20 && abs(event.y - downY) < 20) {

                    if (event.x >= currentLRectF.left && event.x <= currentLRectF.right &&
                        event.y >= currentLRectF.top && event.y <= currentLRectF.bottom
                    ) {
                        if (roleArray.size == 2 && roleArray[0] == "2") {
                            onbotViewClickListener?.homeClick(NORMAL)
                        } else {
                            onbotViewClickListener?.botViewLeft()
                        }
                    }
                    if (event.x >= currentRRectF.left && event.x <= currentRRectF.right &&
                        event.y >= currentRRectF.top && event.y <= currentRRectF.bottom
                    ) {
                        if (roleArray.size == 2 && roleArray[1] == "2") {
                            onbotViewClickListener?.homeClick(NORMAL)
                        } else {
                            onbotViewClickListener?.botViewRight()
                        }
                    }
                    if (event.x >= width / 2 - (homeBitmap.width / 2) && event.x <= width / 2 + (homeBitmap.width / 2) &&
                        event.y >= currentRRectF.top && event.y <= currentRRectF.bottom
                    ) {
                        if (roleArray.size == 2)
                            return false
                        onbotViewClickListener?.apply {
//                            Log.e("qin","${System.currentTimeMillis()- startTime}")

                            if (startTime != 0L && System.currentTimeMillis() - startTime < 500) {
                                return@apply
                            } else {
                                startTime = System.currentTimeMillis()
                            }
//                            Log.e("qin","点击了")

                            when (currentState) {
                                CLOSE -> {
//                                    showStatus(EXPANDED)
//                                    botViewRight(EXPANDED)
//                                    showStatus(NORMAL)
                                    homeClick(NORMAL)
                                }
//                                EXPANDED -> {
//                                    showStatus(CLOSE)
//                                    botViewRight(CLOSE)
//                                }
                                NORMAL -> {
                                    homeClick(CLOSE)

//                                    showStatus(CLOSE)

                                }
                            }
                        }

                    }
                }
            }
        }

        return true
    }

    private var onbotViewClickListener: OnBotViewClickListener? = null

    interface OnBotViewClickListener {
        fun botViewLeft()
        fun botViewRight()
        fun homeClick(@LayerStatus currentState: Int)
    }

    fun setBotViewClickListener(lis: OnBotViewClickListener) {
        onbotViewClickListener = lis
    }


    fun showStatus(@LayerStatus state: Int) {
        if (isAnimValue == true)
            return
        if (currentState == state)
            return
        when (state) {
            CLOSE -> {
                currentStatePath = getClosePath()
                endState = CLOSE
                isClickRightRF = false
            }
//            EXPANDED -> {
//                currentStatePath = getExpandedPath()
//                endState = EXPANDED
////                isClickRightRF = true
//                isClickRightRF = false
//            }
            else -> {
                currentStatePath = getNormalPath()
                endState = NORMAL
//                isClickRightRF = true
                isClickRightRF = false

            }

        }
        valueAnim()
    }

    fun setClickRightBtm() {
        isClickRightRF = true
        isClickLeftRF = false
        invalidate()
    }

    fun setClickLeftBtm() {
        isClickLeftRF = true
        isClickRightRF = false
        invalidate()
    }

    fun setClickHomeBtm() {
        isClickLeftRF = false
        isClickRightRF = false
        invalidate()
    }

    fun valueAnim() {
        val anim = ValueAnimator.ofFloat(0f, 1f).apply {
            duration = 250
            repeatCount = 0

            addUpdateListener {
                animValue = it.animatedValue as Float
                invalidate()

                if (animValue == 1f) {
                    isAnimValue = false
                    currentState = endState
                    onbotViewClickListener?.apply {
//                        botViewRight(currentState)
                    }
                }

            }

        }
        isAnimValue = true
        anim.start()
    }


    fun setTabViewState(string: String) {
        tabViewState(string)
    }

    fun setLabelMsg(msg: String) {
        labelMsg = msg
        invalidate()
    }
}
















